package com.ruoyi.archive.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.archive.domain.SRetiredRedArmy;
import com.ruoyi.archive.domain.SSoldierBaseInfo;
import com.ruoyi.archive.service.ISTransferredCadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.archive.mapper.SDemobilizationSoldierMapper;
import com.ruoyi.archive.domain.SDemobilizationSoldier;
import com.ruoyi.archive.service.ISDemobilizationSoldierService;

/**
 * 复员军人信息Service业务层处理
 *
 * @author Allen
 * @date 2021-09-29
 */
@Service
public class SDemobilizationSoldierServiceImpl implements ISDemobilizationSoldierService {
    @Autowired
    private SDemobilizationSoldierMapper sDemobilizationSoldierMapper;

    @Autowired
    private ISTransferredCadresService transferredCadresService;

    /**
     * 查询复员军人信息
     *
     * @param id
     *         复员军人信息主键
     *
     * @return 复员军人信息
     */
    @Override
    public SDemobilizationSoldier selectSDemobilizationSoldierById(Long id) {
        return sDemobilizationSoldierMapper.selectSDemobilizationSoldierById(id);
    }

    /**
     * 查询复员军人信息列表
     *
     * @param sDemobilizationSoldier
     *         复员军人信息
     *
     * @return 复员军人信息
     */
    @Override
    public List<SDemobilizationSoldier> selectSDemobilizationSoldierList(SDemobilizationSoldier sDemobilizationSoldier) {
        List<SDemobilizationSoldier> demobilizationSoldierList = sDemobilizationSoldierMapper.selectSDemobilizationSoldierList(sDemobilizationSoldier);
        if (CollectionUtil.isNotEmpty(demobilizationSoldierList)) {
            List<Long> soldierIdList = demobilizationSoldierList.stream().map(SDemobilizationSoldier::getSoldierId).collect(Collectors.toList());
            Map<Long, SSoldierBaseInfo> baseInfoMap = transferredCadresService.getSoldierBaseInfoMap(soldierIdList);
            demobilizationSoldierList.stream().forEach(x -> x.setSoldierName(baseInfoMap.get(x.getSoldierId()).getName()));
            demobilizationSoldierList.stream().forEach(x -> x.setIdCard(baseInfoMap.get(x.getSoldierId()).getIdCard()));
        }
        return demobilizationSoldierList;
    }

    /**
     * 新增复员军人信息
     *
     * @param sDemobilizationSoldier
     *         复员军人信息
     *
     * @return 结果
     */
    @Override
    public int insertSDemobilizationSoldier(SDemobilizationSoldier sDemobilizationSoldier) {
        return sDemobilizationSoldierMapper.insertSDemobilizationSoldier(sDemobilizationSoldier);
    }

    /**
     * 修改复员军人信息
     *
     * @param sDemobilizationSoldier
     *         复员军人信息
     *
     * @return 结果
     */
    @Override
    public int updateSDemobilizationSoldier(SDemobilizationSoldier sDemobilizationSoldier) {
        return sDemobilizationSoldierMapper.updateSDemobilizationSoldier(sDemobilizationSoldier);
    }

    /**
     * 批量删除复员军人信息
     *
     * @param ids
     *         需要删除的复员军人信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDemobilizationSoldierByIds(Long[] ids) {
        return sDemobilizationSoldierMapper.deleteSDemobilizationSoldierByIds(ids);
    }

    /**
     * 删除复员军人信息信息
     *
     * @param id
     *         复员军人信息主键
     *
     * @return 结果
     */
    @Override
    public int deleteSDemobilizationSoldierById(Long id) {
        return sDemobilizationSoldierMapper.deleteSDemobilizationSoldierById(id);
    }
}
